$(function() {
    initData(0);
    clear();
    $("#add").dialog({
        autoOpen:false,
        modal:true,
        width:430,
        show:"slide",
        buttons: {
            '确定': function() {
                if ($("#createForm").valid()) {
                    save();
                }
            },
            '取消': function() {
                $(this).dialog('close');
            }
        }
    });

    //表单验证
    $("#createForm").validate({
        rules: {
            login_id: {
                required: true,
                minlength: 2
            },
            name: {
                required: true
            },
            email: {
                required: true,
                email: true
            },
            passwd: {
                required: true,
                minlength: 4
            },
            pwd_sec: {
                required: true,
                minlength: 4,
                equalTo: "#passwd"
            }
        },
        errorPlacement:function(error, element) {
            element.parent().children(".errormsg").html(error);
            if (element.attr('name') == 'pwd_sec') {
                element.parent().children(".pderrormsg").html(error);
            }
        },
        messages: {
            login_id: {
                required:"<span><img src=\"/images/no.gif\">必填项!</span>",
                minlength:"<span><img src=\"/images/no.gif\">长度最少两位!</span>"
            },
            name: "<span><img src=\"/images/no.gif\">必填项!</span>",
            email: {
                required: "<span><img src=\"/images/no.gif\">必填项!</span>",
                email: "<span><img src=\"/images/no.gif\">格式错误!</span>"
            },
            pwd_sec:{
                required: "<span><img src=\"/images/no.gif\">必填项!</span>",
                minlength:"<span><img src=\"/images/no.gif\">长度最少四位!</span>",
                equalTo: "<span><img src=\"/images/no.gif\">确认密码不一置!</span>"
            }
        }
    });
});

function loadData(page, pageSize) {
    var ud_id = $('[name=department]').val();
    var status = $('[name=search_status]').val();
    var login_id = $('[name=loginId]').val();
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",

        url: "/user/user/list", //请求的处理url
        data:{
            'page':page,'pageSize':pageSize,
            'ud_id':ud_id,'status':status,
            'login_id':login_id
        },
        success: function(json) {
            var list = "";
            paginationTotal = json.total;
            if (json.ask != 1) {
                list = "<td colspan='8' class=''>&nbsp;暂无数据</td>";
            } else {
                var i = paginationCurrentPage == 1 ? 1 : paginationPageSize * (paginationCurrentPage - 1) + 1;
                $.each(json.result, function(key, val) {
                    if ((key + 1) % 2 == 1) {
                        list += "<tr class=''>";
                    } else {
                        list += "<tr class='even-tr'>";
                    }
                    list += "<td >" + val.login_id + "</td>";
                    list += "<td >" + val.name + "</td>";
                    list += "<td >" + val.department + "</td>";
                    list += "<td >" + val.position + "</td>";
                    list += "<td >" + val.last_login + "</td>";
                    var statuStr;
                    if (val.status == '1') {
                        statuStr = "<img src='/images/icon/normal.gif'>";
                    } else {
                        statuStr = "<img src='/images/icon/unnormal.gif'>";
                    }
                    list += "<td >" + statuStr + "</td>";
                    list += "<td >" + "<a href='javascript:editRight(" + val.user_id + ")'><img src='/images/icon/authority.png' alt='修改权限'></a>" + "</td>";
                    list += "<td >" + "<a href='javascript:editUser(" + val.user_id + ")'><img src='/images/icon/edit.gif' alt='修改用户信息'></a>" + "</td>";
                    list += "</tr>";
                });
            }
            $("#list").html(list);
        }
    });
}

//添加修改用户信息
function editUser(id) {
    clear();
    if (id) {
        setPwd(this, 0);
        $.ajax({
            type: "POST",
            async: false,
            dataType: "json",
            url: "/user/user/single", //请求的处理url
            data:{'id':id},
            success: function(json) {
                if (json.ask == 1) {
                    var val = json.result;
                    $('[name=user_id]').val(val.user_id);
                    $('[name=login_id]').val(val.login_id);
                    $('[name=name]').val(val.name);
                    $('[name=name_en]').val(val.name_en);
                    $('[name=email]').val(val.email);

                    $('[name=telephone]').val(val.telephone);
                    $('[name=mobile_phone]').val(val.mobile_phone);
                    $('[name=status]').val(val.status);
                    $('[name=ud_id]').val(val.ud_id);
                    $('[name=up_id]').val(val.up_id);
                }
                $("#add").dialog("open");
            }
        });
    } else {
        $("#add").dialog("open");
        setPwd(this, 1);
    }
}

function save() {
    vForm = '';
    vForm = $("#createForm").serializeArray();
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/user/user/edit", //请求的处理url
        data:vForm,
        success: function(json) {
            if (json.ask) {
                alertTip('操作成功');
                loadData(paginationCurrentPage, paginationPageSize);
                $("#add").dialog("close");
            } else {
                if (json.error == null)return;
                var html = '';
                $.each(json.error, function(key, val) {
                    html += '<span><img src="/images/no.gif">' + val + '</span>';
                })
                tip(html);
            }
        }
    });
}

function search() {
    initData(0);
}

function editRight(id, act) {    //这里参数 id 为 user_id
    openIframeDialog('/user/user/rights/id/' + id, 680, 550);
}

function clear() {
    $('[name=user_id]').val('');
    $('[name=login_id]').val('');
    $('[name=loginId]').val('');
    $('#createForm')[0].reset();
    tip('');
}
function tip(mess) {
    var tips = $("#tips");
    tips.html(mess);
    if (mess != '') {
        tips.show();
    } else {
        tips.hide();
    }
    $(".errormsg").each(function() {
        $(this).html('*');
    });
    $(".pderrormsg").html('');
}
//操作提示
function alertTip(tip) {
    $('<div title="提示"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '确定': function() {
                $(this).dialog("close");
            }
        }
    });
}
//重设密码复选框
function setPwd(Obj, val) {
    if (Obj.checked == true || val == 1) {
        $("#createForm input[id='passwd']").removeAttr("disabled").css("background-color", "#fff");
        $("#createForm input[id='pwd_sec']").removeAttr("disabled").css("background-color", "#fff");
        $("#createForm input[id='check_flg']").attr("checked", "true").val(1);
    } else {
        $("#createForm input[id='passwd']").attr("disabled", "disabled").css("background-color", "#ccc");
        $("#createForm input[id='pwd_sec']").attr("disabled", "disabled").css("background-color", "#ccc");
        $("#createForm input[id='check_flg']").removeAttr("checked").val(0);
    }
}